﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class user_duty : ModulePage
    {
  
        private string GetDay(DateTime date)
        {
            string str = string.Empty;
            str = date.ToString("yyyy-MM-dd");
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return (str + "(周日)");

                case DayOfWeek.Monday:
                    return (str + "(周一)");

                case DayOfWeek.Tuesday:
                    return (str + "(周二)");

                case DayOfWeek.Wednesday:
                    return (str + "(周三)");

                case DayOfWeek.Thursday:
                    return (str + "(周四)");

                case DayOfWeek.Friday:
                    return (str + "(周五)");

                case DayOfWeek.Saturday:
                    return (str + "(周六)");
            }
            return str;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int num6;
            DateTime time4;
            DateTime time5;
            DateTime time6;
            string str3;
            string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : base.User.Identity.Name;
            DateTime time = (Request.QueryString["date1"] != null) ? Convert.ToDateTime(Request.QueryString["date1"].ToString()) : DateTime.Now;
            DateTime time2 = (Request.QueryString["date2"] != null) ? Convert.ToDateTime(Request.QueryString["date2"].ToString()) : DateTime.Now;
            this.litUserName.Text = new UserCache(userId).UserName;
            TimeSpan span = new TimeSpan(time.Ticks);
            TimeSpan ts = new TimeSpan(time2.Ticks);
            int num = span.Subtract(ts).Duration().Days + 1;
            this.litNavTitle.Text = "[从" + time.ToString("D") + " 至 " + time2.ToString("D") + " 共" + num.ToString() + "天]";
            int num2 = 30;
            int num3 = 10;
            int num4 = 10;
            int num5 = 60;
            AttendParaDao dao = new AttendParaDao();
            dao.GetPara();
            num2 = dao.DutyIntervalBefore1;
            num3 = dao.DutyIntervalAfter1;
            num4 = dao.DutyIntervalBefore2;
            num5 = dao.DutyIntervalAfter2;
            DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, userId);
            string str2 = (userConfig.Rows[0]["General"] != DBNull.Value) ? userConfig.Rows[0]["General"].ToString() : "";
            if (str2 != string.Empty)
            {
                str2 = "," + str2 + ",";
            }
            DataTable table2 = new DataTable();
            table2.Columns.Add("DutyType", Type.GetType("System.Int32"));
            table2.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("EndTime", Type.GetType("System.DateTime"));
            if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            DataTable table = new DataTable();
            table.Columns.Add("DutyDate", Type.GetType("System.DateTime"));
            for (int i = 1; i <= table2.Rows.Count; i++)
            {
                table.Columns.Add("DutyDesc" + i.ToString(), Type.GetType("System.String"));
            }
            for (int j = 0; j < num; j++)
            {
                table.Rows.Add(new object[] { time.AddDays((double)j) });
                str3 = "SELECT DutyId,RegisterType,RegisterTime,RegisterFlag,Remark FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + userId + "' AND [RegisterTime] >='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " 23:59:59'";
                DataTable table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                for (int n = 0; n < table2.Rows.Count; n++)
                {
                    DataRow[] rowArray = table4.Select("RegisterType = " + table2.Rows[n]["DutyType"].ToString() + " AND RegisterTime >='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["BeginTime"].ToString()).ToString("HH:mm:ss") + "' AND RegisterTime <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["EndTime"].ToString()).ToString("HH:mm:ss") + "'");
                    if (rowArray.Length > 0)
                    {
                        string str4 = Convert.ToDateTime(rowArray[0]["RegisterTime"].ToString()).ToString("HH:mm:ss");
                        if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 1)
                        {
                            str4 = str4 + " 迟到";
                        }
                        else if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 2)
                        {
                            str4 = str4 + " 早退";
                        }
                        if (rowArray[0]["Remark"] != DBNull.Value)
                        {
                            string str5 = str4;
                            str4 = str5 + "<br>备注：" + rowArray[0]["Remark"].ToString() + " <a href=\"javascript:remark('" + rowArray[0]["DutyId"].ToString() + "');\" title=\"修改备注\">修改</a>";
                        }
                        table.Rows[j][n + 1] = str4;
                    }
                    else
                    {
                        str3 = "SELECT [OutId] FROM [dbo].[Attend_Out] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [OutTime1] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [OutTime2]>='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                        DataTable table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                        if (table5.Rows.Count > 0)
                        {
                            table.Rows[j][n + 1] = "<font color='#008000'>外出</font>";
                        }
                        else
                        {
                            str3 = "SELECT [LeaveId] FROM [dbo].[Attend_Leave] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [LeaveDate1] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [LeaveDate2]>='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                            DataTable table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                            if (table6.Rows.Count > 0)
                            {
                                table.Rows[j][n + 1] = "<font color='#008000'>请假</font>";
                            }
                            else
                            {
                                str3 = "SELECT [EvectionId] FROM [dbo].[Attend_Evection] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [EvectionDate1] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [EvectionDate1]>='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                DataTable table7 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                                if (table7.Rows.Count > 0)
                                {
                                    table.Rows[j][n + 1] = "<font color='#008000'>出差</font>";
                                }
                            }
                        }
                    }
                }
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("<table border=\"0\" cellspacing=\"1\" width=\"95%\" class=\"small\" bgcolor=\"#000000\" cellpadding=\"3\" align=\"center\">");
            builder.Append("<tr class=\"TableHeader\">");
            builder.Append("<td nowrap align=\"center\">日期</td>");
            for (int k = 0; k < table2.Rows.Count; k++)
            {
                builder.Append(" <td nowrap align=\"center\">");
                if (Convert.ToInt32(table2.Rows[k]["DutyType"].ToString()) == 1)
                {
                    builder.Append("上班 ");
                }
                else
                {
                    builder.Append("下班 ");
                }
                builder.Append("(" + Convert.ToDateTime(table2.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + ")");
                builder.Append(" </td>");
            }
            builder.Append("</tr>");
            DataView view = new DataView(table);
            string str6 = string.Empty;
            if (table.Columns.Count > 1)
            {
                for (int num11 = 1; num11 < table.Columns.Count; num11++)
                {
                    if (str6 == string.Empty)
                    {
                        str6 = str6 + "DutyDesc" + num11.ToString() + " IS NOT NULL";
                    }
                    else
                    {
                        str6 = str6 + " OR DutyDesc" + num11.ToString() + " IS NOT NULL";
                    }
                }
            }
            if (str6 != string.Empty)
            {
                view.RowFilter = str6;
            }
            for (int m = 0; m < view.Count; m++)
            {
                bool flag;
                int dayOfWeek = (int)Convert.ToDateTime(view[m]["DutyDate"].ToString()).DayOfWeek;
                str3 = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + Convert.ToDateTime(view[m]["DutyDate"].ToString()).ToString("yyyy-MM-dd") + "' AND '" + Convert.ToDateTime(view[m]["DutyDate"].ToString()).ToString("yyyy-MM-dd") + "' <= [EndDate]";
                if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, str3) != null)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                if (flag)
                {
                    builder.Append("<tr class=\"TableContent\">");
                }
                else if (str2.IndexOf("," + dayOfWeek.ToString() + ",") >= 0)
                {
                    builder.Append("<tr class=\"TableContent\">");
                }
                else
                {
                    builder.Append("<tr class=\"TableData\">");
                }
                builder.Append("<td nowrap align=\"center\">" + this.GetDay(Convert.ToDateTime(view[m]["DutyDate"].ToString())) + "</td>");
                for (int num14 = 1; num14 <= table2.Rows.Count; num14++)
                {
                    builder.Append("<td align=\"center\">");
                    if (view[m][num14] == DBNull.Value)
                    {
                        if (flag)
                        {
                            builder.Append("节假日");
                        }
                        else if (str2.IndexOf("," + dayOfWeek.ToString() + ",") >= 0)
                        {
                            builder.Append("公休日");
                        }
                        else
                        {
                            builder.Append("未登记");
                        }
                    }
                    else
                    {
                        builder.Append(view[m][num14].ToString());
                    }
                    builder.Append("</td>");
                }
                builder.Append("</tr>");
            }
            builder.Append("</table>");
            if (view.Count == 0)
            {
                this.litDuty.Text = base.ShowMessage("无上下班登记记录");
            }
            else
            {
                this.litDuty.Text = builder.ToString();
            }
        }
    }
}
